package com.yaojiaxun.logisticsPlatformSystem.service;

import com.baomidou.mybatisplus.plugins.Page;
import com.yaojiaxun.logisticsPlatformSystem.dto.AreaDTO;
import com.yaojiaxun.logisticsPlatformSystem.dto.PageDTO;
import com.yaojiaxun.logisticsPlatformSystem.entity.Area;
import com.yaojiaxun.logisticsPlatformSystem.mapper.AreaMapper;
import com.yaojiaxun.logisticsPlatformSystem.util.TimeFormat;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Auther: yjx_j
 * @Date: 2019/1/13 14:38
 * @Description:
 */
@Service
public class AreaService {
    @Autowired
    private AreaMapper areaMapper;

    public List<Area> findAllArea(){
        return areaMapper.findAllArea();
    }

    //查找所有省级区域（city_level=2）
    public List<Area> findAllProvinces(){
        return areaMapper.findAllProvinces();
    }

    //查找所有市级区域（city_level=3）
    public List<Area> findAllCities(){
        return areaMapper.findAllCities();
    }

    //查找所有县级区域（city_level=4）
    public List<Area> findAllCountries(){
        return areaMapper.findAllCountries();
    }

    public AreaDTO beanToDTO(Area area){
        if (area == null)
            return null;
        AreaDTO areaDTO = new AreaDTO();
        BeanUtils.copyProperties(area, areaDTO);
        areaDTO.setUpdateTime(TimeFormat.convertTime(area.getUpdateTime()));
        areaDTO.setCreateTime(TimeFormat.convertTime(area.getCreateTime()));
        return areaDTO;
    }

    public PageDTO<AreaDTO> findPage(Integer pageNum){
        PageDTO<AreaDTO> pageDTO = new PageDTO<>();
        Page<AreaDTO> page = new Page<>(pageNum, 32);
        List<AreaDTO> result = areaMapper.selectPage(page,null).stream().map(this::beanToDTO).collect(Collectors.toList());
        result.remove(0);
        pageDTO.setPage(page);
        pageDTO.setResult(result);
        return pageDTO;
    }

    public List<Area> findCityLevelByParentId(Integer parentId){
        return areaMapper.findCityLevelByParentId(parentId);
    }

    public List<Area> findCountryLevelByParentId(Integer parentId){
        return areaMapper.findCountryLevelByParentId(parentId);
    }

    public String findCityNameById(Integer id){
        return areaMapper.findCityNameById(id);
    }

    public Integer findParentIdById(Integer id){
        return areaMapper.findParentIdById(id);
    }

    public Integer findAreaIdByName(String name)
    {
        return areaMapper.findAreaIdByName(name);
    }

    public List<Area> findCitySpecial(){
        return areaMapper.findCitySpecial();
    }
}
